package com.clas.liu.myapplication.view.activity.impl;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.info.Info;
import com.clas.liu.myapplication.model.entity.user.UserData;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;
import com.clas.liu.myapplication.presenter.impl.InfoPresenter;
import com.clas.liu.myapplication.view.activity.i.IInfoActivity;
import com.clas.liu.myapplication.view.util.DateUtil;
import com.clas.liu.myapplication.view.util.SaveUtil;
import com.clas.liu.myapplication.view.util.SomeResources;
import com.clas.liu.myapplication.view.widget.CircleImageView;
import com.clas.liu.myapplication.view.widget.ItemImageView;
import com.hyphenate.easeui.EaseConstant;
import com.squareup.picasso.Picasso;

import java.util.Date;

/**
 * Created by liu on 2019/5/13.
 */

public class InfoActivity extends BaseActivity<InfoPresenter> implements View.OnClickListener,IInfoActivity {
    private ImageView back;
    private CircleImageView head;
    private TextView name;
    private ImageView sex;
    private TextView category;
    private TextView time;
    private TextView text;
    private ItemImageView pic;
    private TextView key;
    private LinearLayout chat;
    private LinearLayout jubao;
    private RelativeLayout status;
    private LinearLayout bottomLayout;
    private Info info;
    private AlertDialog.Builder biulder;
    private AlertDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_layout);
        findView();
        init();
        setListener();
    }

    private void init() {
        info = (Info) getIntent().getSerializableExtra("info");
        if (info.getUser_id() == SaveUtil.<UserData>getFromLocal(getApplicationContext(),"user").getId())
            bottomLayout.setVisibility(View.GONE);
        name.setText(info.getName());
        Picasso.with(this).load(info.getHead()).placeholder(R.mipmap.default_head).
                error(R.mipmap.loading).into(head);
        category.setText(SomeResources.getCategorys().get(info.getCategory()));
        category.setBackgroundResource(SomeResources.getCategoryDrawables().get(info.getCategory()));
        if (info.getSex().equals("男"))
            sex.setImageResource(R.mipmap.man);
        else
            sex.setImageResource(R.mipmap.woman);
        Date date = new Date(Long.parseLong(info.getRelease_time()));
        time.setText(DateUtil.getFormatTime(date));
        text.setText(info.getText());
        if (info.getPic() != null && !info.getPic().equals("") && !info.getPic().equals("null")) {
            Log.i("test", "onBindViewHolder: " + info.getName() + info.getPic());
            pic.setVisibility(View.VISIBLE);
            Picasso.with(this).load(info.getPic()).placeholder(R.mipmap.loading).
                    error(R.mipmap.loading).into(pic);
        } else pic.setVisibility(View.GONE);
        if (info.getKey_word() != null && !info.getKey_word().equals("") && !info.getKey_word().equals("null")) {
            key.setVisibility(View.VISIBLE);
            key.setText(new StringBuilder().append("关键词： ").append(info.getKey_word()).toString());
        } else key.setVisibility(View.GONE);
        if (info.getIs_settled() == 0) {
            status.setVisibility(View.GONE);
        } else {
            status.setVisibility(View.VISIBLE);
        }

    }

    private void findView() {
        back = findViewById(R.id.info_back_bt);
        head = findViewById(R.id.info_head);
        name = findViewById(R.id.info_name);
        sex = findViewById(R.id.info_sex);
        category = findViewById(R.id.info_category);
        text = findViewById(R.id.info_text);
        pic = findViewById(R.id.info_pic);
        key = findViewById(R.id.info_key);
        chat = findViewById(R.id.info_chat);
        jubao = findViewById(R.id.info_jubao);
        time = findViewById(R.id.info_time);
        status = findViewById(R.id.info_status);
        bottomLayout = findViewById(R.id.info_bottom_layout);

    }

    private void setListener() {
        back.setOnClickListener(this);
        head.setOnClickListener(this);
        pic.setOnClickListener(this);
        jubao.setOnClickListener(this);
        chat.setOnClickListener(this);
    }

    public void showTips(String tips) {
        if (biulder == null)
            biulder = new AlertDialog.Builder(this);
        biulder.setMessage(tips);
        biulder.setPositiveButton(R.string.alertdialog_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.setMessage("正在请求中......");
                mPresenter.report(String.valueOf(info.getId()),String.valueOf(System.currentTimeMillis()));
            }
        });
        biulder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog = biulder.show();
    }

    @Override
    protected InfoPresenter getPresenter() {
        return new InfoPresenter(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.info_back_bt:
                finish();
                break;
            case R.id.info_head:
                intent = new Intent(this, UserInfoActivity.class);
                intent.putExtra("user_id", info.getUser_id());
                startActivity(intent);
                break;
            case R.id.info_pic:
                intent = new Intent(this, ImageViewActivity.class);
                intent.putExtra("url", info.getPic());
                startActivity(intent);
                break;
            case R.id.info_jubao:
                showTips("确定举报该消息？？");
                break;
            case R.id.info_chat:
                intent = new Intent(this, ChatActivity.class);
                intent.putExtra(EaseConstant.EXTRA_USER_ID, String.valueOf(info.getUser_id()));
                intent.putExtra("name", info.getName());
                intent.putExtra("head", info.getHead());
                startActivity(intent);
                break;

        }
    }

    @Override
    public void failBecauseNetReturn(int code) {
        showToast(code+"请求失败！", Toast.LENGTH_SHORT);
        dialog.dismiss();
    }

    @Override
    public void reportReturn() {
        showToast("举报成功!",Toast.LENGTH_SHORT);
        dialog.dismiss();
    }
}
